package com.zhai.domain.activity.service.partake;

import com.alibaba.fastjson.JSON;
import com.zhai.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import com.zhai.domain.activity.model.entity.ActivityEntity;
import com.zhai.domain.activity.model.entity.PartakeRaffleActivityEntity;
import com.zhai.domain.activity.model.entity.UserRaffleOrderEntity;
import com.zhai.domain.activity.model.valobj.ActivityStateVO;
import com.zhai.domain.activity.repository.IActivityRepository;
import com.zhai.domain.activity.service.IRaffleActivityPartakeService;
import com.zhai.types.enums.ResponseCode;
import com.zhai.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 参与抽奖活动抽象类
 * @ClassName: AbstractRaffleActivityPartake
 * @Author: zhaiyongxin
 * @Date: 2025/2/27 14:34
 * @Version: 1.0
 */
@Slf4j
public abstract class AbstractRaffleActivityPartake implements IRaffleActivityPartakeService {

    protected final IActivityRepository activityRepository;

    public AbstractRaffleActivityPartake(IActivityRepository activityRepository) {
        this.activityRepository = activityRepository;
    }

    /**
     * 创建抽奖单
     *
     * @param partakeRaffleActivityEntity
     */
    @Override
    public UserRaffleOrderEntity createOrder(PartakeRaffleActivityEntity partakeRaffleActivityEntity) {
        Long activityId = partakeRaffleActivityEntity.getActivityId();
        String userId = partakeRaffleActivityEntity.getUserId();
        Date currentDate = new Date();
        // 1.获取活动信息
        ActivityEntity activityEntity = activityRepository.queryRaffleActivityByActivityId(activityId);

        // 2.校验活动生效状态
        ActivityStateVO activityState = activityEntity.getState();
        if (!ActivityStateVO.open.equals(activityState)) {
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }

        // 3.校验活动有效期
        Date beginDateTime = activityEntity.getBeginDateTime();
        Date endDateTime = activityEntity.getEndDateTime();
        if (currentDate.before(beginDateTime) || currentDate.after(endDateTime)) {
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        // 4.查询是否有使用过的订单
        UserRaffleOrderEntity userRaffleOrderEntity = activityRepository.queryUnusedActivityOrder(partakeRaffleActivityEntity);
        if (userRaffleOrderEntity != null) {
            log.info("创建参与活动订单 userId:{} activityId:{} userRaffleOrderEntity:{}", userId, activityId, JSON.toJSONString(userRaffleOrderEntity));
            return userRaffleOrderEntity;
        }

        // 5.账户额度过滤
        CreatePartakeOrderAggregate createPartakeOrderAggregate = this.doFilterAccount(activityId, userId, currentDate);

        // 6.创建订单
        UserRaffleOrderEntity userRaffleOrder = this.buildUserRaffleOrder(userId, activityId, currentDate);

        createPartakeOrderAggregate.setUserRaffleOrderEntity(userRaffleOrder);

        // 7.保存聚合对象
        activityRepository.saveCreatePartakeOrderAggregate(createPartakeOrderAggregate);

        return userRaffleOrder;
    }

    /**
     * 创建抽奖单
     *
     * @param userId
     * @param activityId
     */
    @Override
    public UserRaffleOrderEntity createOrder(String userId, Long activityId) {
        return createOrder(PartakeRaffleActivityEntity
                .builder()
                .activityId(activityId)
                .userId(userId)
                .build());
    }

    protected abstract UserRaffleOrderEntity buildUserRaffleOrder(String userId, Long activityId, Date currentDate);

    protected abstract CreatePartakeOrderAggregate doFilterAccount(Long activityId, String userId, Date currentDate);
}
